package com.another.bidservice.service.impl;

import com.another.bidservice.dao.jpa.ColumnJPADao;
import com.another.bidservice.entity.TColumn;
import com.another.bidservice.entity.TForm;
import com.another.bidservice.dao.jpa.FormJPADao;
import com.another.bidservice.dao.mapper.FormDao;
import com.another.bidservice.service.IFormService;
import com.another.bidservice.utils.util.sql.Factory;
import com.another.common.utils.Page;
import com.another.common.utils.common.EmptyUtil;
import com.another.common.utils.common.MyException;
import com.another.common.utils.result.Result;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class FormServiceImpl extends BaseServiceImpl implements IFormService {

    private Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private FormDao formDao;

    @Autowired
    private FormJPADao formJPADao;

    @Autowired
    private ColumnJPADao columnJPADao;

    @Override
    public Result getList(TForm tForm) {
        String sql = "select * from db_sys.vt_form";
        List<TForm> list = formDao.getList(sql);
        if (EmptyUtil.isEmpty(list)) {
            return Result.success("没有查询到数据！", "");
        } else {
            return Result.success(list);
        }
    }


    @Override
    public Result insertList(List<TForm> tForms) {
        int ret = formDao.insertList(tForms);
        if (ret == tForms.size()) {
            return Result.success("添加成功！", "");
        } else {
            throw new MyException(101, "添加失败！");
        }
    }


    @Override
    public Result deleteList(List<Integer> rowid) {
        int ret = formDao.deleteList(rowid);
        if (ret == rowid.size()) {
            return Result.success("删除成功！", "");
        } else {
            throw new MyException(101, "删除失败！");
        }
    }

    @Override
    public Result updateList(List<TForm> tForms) {
        try {
            int ret = formDao.updateList(tForms);
            return Result.success("修改成功！", "");
        } catch (Exception e) {
            logger.info(e.getMessage(), e);
            throw new MyException(101, "修改失败！");
        }
    }


    @Override
    public Result insertForm(TForm tForm) {
        try {
            int ret = formDao.insertForm(tForm);
            return Result.success("添加成功！", "");
        } catch (Exception e) {
            logger.info(e.getMessage(), e);
            throw new MyException(101, "添加失败！");
        }
    }

    @Override
    public Result updateForm(TForm tForm) {
        try {
            int ret = formDao.updateForm(tForm);
            return Result.success("修改成功！", "");
        } catch (Exception e) {
            logger.info(e.getMessage(), e);
            throw new MyException(101, "修改失败！");
        }
    }

    @Override
    public Result deleteForm(TForm tForm) {
        try {
            int ret = formDao.deleteForm(tForm);
            return Result.success("删除成功！", "");
        } catch (Exception e) {
            logger.info(e.getMessage(), e);
            throw new MyException(101, "删除失败！");
        }
    }

    /**
     * @param form
     * @return com.another.exhibitioncommon.utils.result.Result
     * @author Another
     * @description :  公共接口
     * @date 2020/3/13 15:56:21
     * @version : 1.0
     */
    @Override
    public Result publicAPI(TForm form) {

        Page<Map<String, String>> page = new Page<>();

        if (EmptyUtil.isEmpty(form.getFormId())) {
            return Result.error("formId必传!");
        }

        //获取需要显示的列和sql
        TForm sql = formJPADao.findTopByFormId(form.getFormId());
        if (EmptyUtil.isEmpty(sql)) {
            return Result.error("formId是不是传错了!");
        }

        List<TColumn> columns = columnJPADao.findAllByFormId(form.getFormId());
        if (EmptyUtil.isEmpty(columns)) {
            return Result.error("没有查询到需要显示的字段!");
        }

        sql.setColumns(columns);
        sql.setPage(form.getPage());
        sql.setLength(form.getLength());


        //查询总记录数
        String countSql = Factory.SqlCountFactory(sql);
        Object count = formDao.publicCountSql(countSql);
        sql.setTotal(new BigInteger(count.toString()));

        //查询数据
        String dataSql = Factory.SqlDataFactory(sql);
        List<Map<String,String>> data = formDao.publicSql(dataSql);

        if (EmptyUtil.isEmpty(data)) {
            return Result.setSuccessMsg("没有查询到数据!");
        }

        page.setTotal(new BigInteger(count.toString()));
        page.setTotalPages(form.getTotalPages());
        page.setData(data);
        return Result.success(page);
    }
}



